const system = require('./system');
const log = require('electron-log');
const { app, remote } = require('electron');
const path = require('path')

// let exePath = path.dirname(app.getPath('exe')) // 获取到安装目录的文件夹名称
// if(system.isDev()){
//     exePath = path.dirname(app.getPath('downloads'));
// }
let getLogPath = function(){
    const date = new Date();
    const dstr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
    let logPath = path.join(system.getHomePath(), `logs/${dstr}_info.log`);
    // console.log(`logpath=${logPath}`);
    return logPath;
};

log.transports.file.level = 'info'
log.transports.file.maxSize = 5 * 1024 * 1024; // 5 MB
log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] {scope} {text}' // 设置文件内容格式
// log.transports.file.file = __dirname + '/logs/info.log';
log.transports.file.datePattern ='YYYY-MM-DD'
log.transports.file.resolvePathFn = () => {
   let logPath =  getLogPath();
    // log.info(`logPath=${logPath}`)
   return logPath;
}

Object.assign(console, log.functions);


// log.transports.file.level = false;
// log.transports.console.level = false;

// log.transports.file.archiveLog = (oldLogPath) => {
//     const date = new Date();
//     const archivePath = `${__dirname}/logs/archive/info_${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}.log`;
//     return archivePath;
// };
log.transports.file.archiveLogMaxDays = 7;


//print init info
log.info(`homePath=${system.getHomePath()}`)
log.info(`appPath=${system.getAppPath()}`)
log.info(`logPath=${getLogPath()}`)
// 将日志输出到控制台和 DevTools
// log.transports.console.level = 'debug';


// let date = new Date()
// date = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
// log.transports.file.fileName = date + '.log' // 创建文件名格式为 '时间.log' (2023-02-01.log)
//
// // 可以将文件放置到指定文件夹中，例如放到安装包文件夹中
// const path = require('path')
// const exePath = path.dirname(remote.app.getPath('exe')) // 获取到安装目录的文件夹名称
// // 指定日志文件夹位置
// log.transports.file.resolvePath = ()=> exePath+'\\'+'logs\\'+date+'.log'

// 有六个日志级别error, warn, info, verbose, debug, silly。默认是silly

module.exports = {
    info (param) {
        log.info(param)
    },
    warn (param) {
        log.warn(param)
    },
    error (param) {
        log.error(param)
    },
    debug (param) {
        log.debug(param)
    },
    verbose (param) {
        log.verbose(param)
    },
    silly (param) {
        log.silly(param)
    },

};

